package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.LoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/htuser")
public class HtController {

    @Autowired
    private LoginUserService loginUserService;
    //查询所有用户√

    @RequestMapping("/findUser")
    public String  users(Model model){
        List<User> users = loginUserService.users();
        Model model1 = model.addAttribute("users", users);
        System.out.println(model1);
        return "userlistindex";//后台查询所有用户
    }
    //更具id查询√
    @RequestMapping("/updateUser")
    public String updateUser(Model model,int id){
        /*loginUserService.findUserById(id);*/
        User user = loginUserService.updateUser2(id);
        System.out.println(user+"88888888888888888888888888888888888");
        Model model1 = model.addAttribute("user", user);
        System.out.println(model1+"========================================================");
        return "updata";//后台修改用户界面
    }
    //修改
    @RequestMapping("/updateUser2")
    public String updateUser2(User user){
        //根据id修改其他值

        loginUserService.updateUser(user);
        return "redirect:/htuser/findUser";//从定向到用户列表
    }
    //删除√
    @RequestMapping("/delete")
    public String delete(int id){
        loginUserService.getDelect(id);
        /*return "redirect:/user/findUser";*/
        return "redirect:/htuser/findUser";//后台用户列表
    }
    @RequestMapping("/tianjia")
    public String tinajia(){
        return "insert1";
    }
    //添加用户
    @RequestMapping(value = "/tianjiayonghu",method = RequestMethod.POST)
    public String zhuce(User user){
        User userByName = loginUserService.findUserByName(user.getUsername());
        if(userByName!=null){
            return "yhmcz";//用户名存在
        }
        /*if(userByName.getUsername()!=null){
        }*/
        int i = loginUserService.zhuCe(user);
        return "redirect:/htuser/findUser";//跳转到登陆页

    }

}
